package com.hnqcgczy.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

public class MathCaptchaGenerator {

    private static final int WIDTH = 120; // 图片宽度
    private static final int HEIGHT = 35; // 图片高度
    private static final int FONT_SIZE = 18; // 字体大小
    private static final String FONT_NAME = "Arial"; // 字体名称

    public static class MathCaptcha {
        private final String expression; // 验证码表达式
        private final int result; // 验证码结果

        public MathCaptcha(String expression, int result) {
            this.expression = expression;
            this.result = result;
        }

        public String getExpression() {
            return expression;
        }

        public int getResult() {
            return result;
        }
    }

    // 生成随机的加减法表达式
    public static MathCaptcha generateCaptcha() {
        Random random = new Random();
        int num1 = random.nextInt(21); // 0到20
        int num2 = random.nextInt(21); // 0到20
        char operator;
        int result;

        if (random.nextBoolean()) {
            operator = '+'; // 加法
            result = num1 + num2;
        } else {
            operator = '-'; // 减法
            if (num1 < num2) { // 确保结果不为负
                int temp = num1;
                num1 = num2;
                num2 = temp;
            }
            result = num1 - num2;
        }

        String expression = num1 + " " + operator + " " + num2 + " = ?";
        return new MathCaptcha(expression, result);
    }

    // 创建验证码图片
    public static BufferedImage createCaptchaImage(String text) {
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();

        // 设置背景和抗锯齿
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, WIDTH, HEIGHT);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // 绘制文本
        g.setFont(new Font(FONT_NAME, Font.PLAIN, FONT_SIZE));
        g.setColor(Color.BLACK);
        g.drawString(text, 10, 30); // 设置文本位置

        // 添加干扰线
        Random random = new Random();
        g.setColor(Color.GRAY);
        for (int i = 0; i < 10; i++) {
            int x1 = random.nextInt(WIDTH);
            int y1 = random.nextInt(HEIGHT);
            int x2 = random.nextInt(WIDTH);
            int y2 = random.nextInt(HEIGHT);
            g.drawLine(x1, y1, x2, y2); // 绘制随机干扰线
        }

        g.dispose();
        return image;
    }

    // 保存验证码图片
    public static void saveCaptchaImage(BufferedImage image, String filename) {
        try {
            ImageIO.write(image, "png", new File(filename)); // 保存为PNG格式
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 示例代码
    public static void main(String[] args) {
        MathCaptcha captcha = generateCaptcha(); // 生成随机加减法表达式
        BufferedImage image = createCaptchaImage(captcha.getExpression()); // 创建验证码图片
        saveCaptchaImage(image, "captcha.png"); // 保存验证码图片
        System.out.println("验证码表达式: " + captcha.getExpression());
        System.out.println("验证码结果: " + captcha.getResult());
    }
}
